<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :before-close="closeDialog"
    :close-on-click-modal="false"
    width="30%"
  >
    <el-form
      ref="form"
      label-position="top"
      :model="form"
    >
      <el-form-item :label="$t('DisplayName:LockoutDuration')" prop="lockoutDuration">
        <el-input v-model="form.lockoutDuration" type="number" />
      </el-form-item>
      <el-form-item :label="$t('DisplayName:LockDurationType')" prop="lockDurationType">
        <el-select v-model="form.lockDurationType">
          <el-option :label="$t('Enum:LockDurationType.Second')" value="s" />
          <el-option :label="$t('Enum:LockDurationType.Minute')" value="m" />
          <el-option :label="$t('Enum:LockDurationType.Hour')" value="h" />
          <el-option :label="$t('Enum:LockDurationType.Day')" value="d" />
          <el-option :label="$t('Enum:LockDurationType.Month')" value="M" />
          <el-option :label="$t('Enum:LockDurationType.Year')" value="y" />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">{{ $t('AbpIdentity[\'Cancel\']') }}</el-button>
      <el-button type="primary" @click="submitForm">
        <i class="el-icon-check" />
        {{ $t('AbpIdentity[\'Save\']') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>

import {
  lockUser
} from '@/api/administration/user/user'
import moment from 'moment'
export default {
  name: 'LockUser',
  props: {
    context: {
      type: Object,
      required: true
    },
    visible: {
      type: Boolean,
      required: true,
      default: false
    }
  },
  data() {
    return {
      title: '',
      form: {
        id: '',
        lockDurationType: 's',
        lockoutDuration: 0
      }
    }
  },
  watch: {
    context(newVal, oldVal) {
      this.title = `${this.$t('AbpIdentity[\'Lock\']')} - ${newVal.user.userName}`
    }
  },
  created() {
  },
  methods: {
    async submitForm() {
      const seconds = moment.duration(this.form.lockoutDuration, this.form.lockDurationType).as('s')
      await lockUser(this.context.user.id, seconds)
      this.$message({ message: this.$t('AbpIdentity[\'Ok\']'), type: 'success' })
      this.$emit('succeed', this.context.user, seconds)
      this.closeDialog()
    },
    closeDialog() {
      this.resetDialog() // 重置表单
      this.$emit('update:visible', false)
    },
    resetDialog() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>
</style>
